#!/usr/bin/expect -f
set mailserver [lrange $argv 0 0]
set timeout 5
spawn telnet $mailserver 25
expect "failed" {
                send_user "$to:$mailserver:connect failed\n"
		send -- \x1d
		send "quit"
		close -onexec 1
		wait
		send_user "$to:$mailserver exit\n"
		exit 1    
        } "2?? *" {
        } "4?? *" {
		send_user "$to:$mailserver:connect return 4xx error\n"
                send "QUIT\r"
		send -- \x1d
		send "quit"
		close -onexec 1
		wait
		send_user "$to:$mailserver exit\n"
		exit 1
        } "5?? *" {
		send_user "$to:$mailserver:connect return 5xx error\n"
                send "QUIT\r"
		send -- \x1d
		send "quit"
		close -onexec 1
		wait
		send_user "$to:$mailserver exit\n"
		exit 1
        } "refused" {
                send_user "$to:$mailserver: connect refused\n"
		send -- \x1d
		send "quit"
		close -onexec 1
		wait
		send_user "$to:$mailserver exit\n"
		exit 1
        } "closed" {
                send_user "$to:$mailserver: connect closed\n"
		send -- \x1d
		send "quit"
		close -onexec 1
		wait
		send_user "$to:$mailserver exit\n"
		exit 1
        } timeout {
                send_user "$to:$mailserver: connect to port 25 timeout\n"
                send "QUIT\r"
		send -- \x1d
		send "quit"
		close -onexec 1
		wait
		send_user "$to:$mailserver exit\n"
		exit 1
        }
send \035
expect "telnet>" {
		send_user "quit\r"
		send "quit\r"
	} timeout {
		send_user "timeout"
	} 
close -onexec 1
wait
exit
